home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / usr (gcc 1.37 libs) / mac / open.c < prev    next >
C/C++ Source or Header  |  1993-12-08  |  2KB  |  75 lines

  1. #include <sys/types.h>
  2. #include <sys/syslimits.h>
  3. #include <fcntl.h>
  4. #include "crtlocal.h"
  5.  
  6. int chown(const char *name, uid_t owner, gid_t group)
  7.         {
  8.         OSErr   err;
  9.         HFileParam pb;
  10.         pb.ioNamePtr = cnv_unix_name(name);
  11.         pb.ioVRefNum = crt_ioVRefNum;
  12.         pb.ioDirID = 0;
  13.         pb.ioFlCrDat = 0;
  14.         pb.ioFlMdDat = 0;
  15.         err = PBHGetFInfoSync((HParmBlkPtr) &pb );
  16.         if (!err)
  17.             {
  18.             pb.ioFlFndrInfo.fdCreator = owner;
  19.             pb.ioFlFndrInfo.fdType = group;
  20.             err = PBHSetFInfoSync((HParmBlkPtr) &pb );
  21.             }
  22.         return err?-1:0;
  23.         }
  24.     
  25. int open(const char *name, int flags, ...)
  26.     {
  27.     OSErr err;
  28.     IOParam pb;
  29.     int fd,refnum;
  30.     mysleep(1);
  31.     if (flags & O_CREAT) 
  32.         {
  33.         unlink(name);
  34.         pb.ioNamePtr = cnv_unix_name(name);
  35.         pb.ioVRefNum = crt_ioVRefNum;
  36.         pb.ioVersNum = 0;
  37.         err = PBCreateSync((ParmBlkPtr) &pb);
  38.         if (!err) chown(name, 'CPED', 'TEXT');
  39.         }
  40.     pb.ioNamePtr = cnv_unix_name(name);
  41.     pb.ioVRefNum = crt_ioVRefNum;
  42.     pb.ioVersNum = 0;
  43.     pb.ioPermssn = flags&(O_WRONLY|O_RDWR)?fsRdWrPerm:fsRdPerm;
  44.     pb.ioMisc = 0;
  45.     if (flags&O_RESOURCE)
  46.             err = PBOpenRFSync((ParmBlkPtr) &pb);
  47.     else
  48.             err = PBOpenSync((ParmBlkPtr) &pb);
  49.     if ( err ) return -1;
  50.     refnum = pb.ioRefNum;
  51.     if (flags & O_APPEND)
  52.         {
  53.         pb.ioRefNum = refnum;
  54.         pb.ioPosMode = fsFromLEOF;
  55.         pb.ioPosOffset = 0;
  56.         (void)PBSetFPosSync((ParmBlkPtr)&pb);
  57.         }
  58.     if (flags & O_TRUNC)
  59.         {
  60.         pb.ioCompletion = 0;
  61.         pb.ioRefNum = refnum;
  62.         pb.ioMisc = 0;
  63.         PBSetEOFSync((ParmBlkPtr)&pb);
  64.         }
  65.     fd = next_fd(3);
  66.     crt_fd_tab[fd].fd = refnum;
  67.     crt_fd_tab[fd].flags = flags;
  68.     return fd;
  69.     }
  70.  
  71. int    creat(const char *name, mode_t mode)
  72.     {
  73.     return open(name, O_CREAT|O_TRUNC|O_WRONLY, mode);
  74.     }
  75.